package com.moarea.app.controller.system.querys;

import com.moarea.app.file.imp.FileHandlerImpl;
import com.moarea.app.file.res.FileContentType;
import com.moarea.app.file.res.FileExecuteType;
import com.moarea.app.file.res.FilePathType;
import com.moarea.app.lang.model.ResultObject;
import com.moarea.app.model.BaseCommonquery;
import com.moarea.app.model.BaseCommonqueryData;
import com.moarea.app.model.BaseCommonquerycondition;
import com.moarea.app.model.BaseCommonqueryresult;
import com.moarea.app.mybatis.model.PagedObject;
import com.moarea.app.mybatis.model.ReqPagedObject;
import com.moarea.app.service.impl.querys.BaseCommonqueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by samsung on 2017/4/10.
 */
@Controller
@RequestMapping("/baseCommonqueryController")
public class BaseCommonqueryController {
    @Autowired
    private BaseCommonqueryService commonqueryService;

    /**
     * 获取出所有的合同模版类型及相关的模版
     *
     * @param session HttpSession对象
     * @return 菜单数据集合
     */
    @RequestMapping("/listCommonQueryTypes")
    @ResponseBody
    public ResultObject listCommonQueryTypes(HttpSession session) {
        return commonqueryService.listCommonQueryTypes(session);
    }

    /**
     * 保存模板信息
     *
     * @param baseCommonqueryData
     * @param session
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public ResultObject save(@RequestBody BaseCommonqueryData baseCommonqueryData, HttpSession session) {
        return commonqueryService.save(baseCommonqueryData, session);
    }

    /**
     * 树节点的复制功能
     *
     * @param param
     * @param session
     * @return
     */
    @RequestMapping("copy")
    @ResponseBody
    public ResultObject copy(@RequestBody Map<String, Object> param, HttpSession session) {
        return commonqueryService.copy(param, session);
    }

    /**
     * 导出功能的实现
     *
     * @param param
     * @param session
     * @return
     */
    @RequestMapping("export")
    @ResponseBody
    public ResultObject export(@RequestBody Map<String, Object> param, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        return commonqueryService.export(param, request, response, session);
    }

    /**
     * 更新模板信息
     *
     * @param baseCommonqueryData
     * @param session
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultObject update(@RequestBody BaseCommonqueryData baseCommonqueryData, HttpSession session) {
        return commonqueryService.update(baseCommonqueryData, session);
    }

    /**
     * 树节点排序功能，及更新操作
     *
     * @param baseCommonquerys
     * @param session
     * @return
     */
    @RequestMapping("updateList")
    @ResponseBody
    public ResultObject updateList(@RequestBody List<BaseCommonquery> baseCommonquerys, HttpSession session) {
        return commonqueryService.updateList(baseCommonquerys, session);
    }

    /**
     * 结果排序，及结果表批量更新
     *
     * @param baseCommonqueryresults
     * @param session
     * @return
     */
    @RequestMapping("updateResultList")
    @ResponseBody
    public ResultObject updateResultList(@RequestBody List<BaseCommonqueryresult> baseCommonqueryresults, HttpSession session) {
        return commonqueryService.updateResultList(baseCommonqueryresults, session);
    }

    /**
     * 条件排序，及条件表批量更新
     *
     * @param session
     * @return
     */
    @RequestMapping("updateConditiontList")
    @ResponseBody
    public ResultObject updateConditiontList(@RequestBody List<BaseCommonquerycondition> baseCommonqueryconditions, HttpSession session) {
        return commonqueryService.updateConditiontList(baseCommonqueryconditions, session);
    }

    /**
     * 根据ID获取实体
     *
     * @param baseCommonquery
     * @param session
     * @return
     */
    @RequestMapping("getEntityById")
    @ResponseBody
    public ResultObject getEntityById(@RequestBody BaseCommonquery baseCommonquery, HttpSession session) {
        return commonqueryService.getEntityById(baseCommonquery, session);
    }

    /**
     * 删除操作
     *
     * @param baseCommonquery
     * @param session
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultObject delete(@RequestBody BaseCommonquery baseCommonquery, HttpSession session) {
        return commonqueryService.deleteCommonquery(baseCommonquery);
    }

    @RequestMapping("deleteCondition")
    @ResponseBody
    public ResultObject deleteCondition(@RequestBody BaseCommonquerycondition baseCommonquery, HttpSession session) {
        return commonqueryService.deleteCondition(baseCommonquery);
    }

    @RequestMapping("deleteResult")
    @ResponseBody
    public ResultObject deleteResult(@RequestBody BaseCommonqueryresult baseCommonquery, HttpSession session) {
        return commonqueryService.deleteResult(baseCommonquery);
    }

    /**
     * 根据配置的sql为下拉框赋值
     *
     * @param param
     * @param session
     * @return
     */
    @RequestMapping("getSelectOption")
    @ResponseBody
    public ResultObject getSelectOption(@RequestBody Map<String, Object> param, HttpSession session) {
        return commonqueryService.getSelectOption(param, session);
    }

    /**
     * 获取商品分类下拉数据
     *
     * @return
     */
    @RequestMapping(value = "/listGoodsTypesForSelect", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject listGoodsTypesForSelect(@RequestBody Map<String, Object> param, HttpSession session) {
        return commonqueryService.filterListGoodsTypesForSelect(session, param);
    }

    @RequestMapping("checkIsExist")
    @ResponseBody
    public ResultObject checkIsExist(@RequestBody Map<String, Object> param, HttpSession session) {
        return commonqueryService.checkIsExist(param);
    }

    /**
     * 根据表头的ID获取 结果信息
     *
     * @param formValues
     * @param session
     * @return
     */
    @RequestMapping("listCommonQueryResults")
    @ResponseBody
    public PagedObject<List<Map<String, Object>>> listCommonQueryResults(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return commonqueryService.listCommonQueryResults(formValues.getData(), formValues.getPage(), formValues.getLimit(), formValues.getAllcount(), session);
    }

    /**
     * 根据表头的ID获取查询信息
     *
     * @param formValues
     * @param session
     * @return
     */
    @RequestMapping("listCommonQueryConditions")
    @ResponseBody
    public PagedObject<List<Map<String, Object>>> listCommonQueryConditions(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return commonqueryService.listCommonQueryConditions(formValues.getData(), formValues.getPage(), formValues.getLimit(), formValues.getAllcount(), session);
    }

    /**
     * 根据页面配置的url参数获取模板信息
     *
     * @param param
     * @param session
     * @return
     */
    @RequestMapping("listCommonQueryTempleteData")
    @ResponseBody
    public ResultObject listCommonQueryTempleteData(@RequestBody Map<String, Object> param, HttpSession session) {
        return commonqueryService.listCommonQueryTempleteData(param, session);
    }

    /**
     * 模板通用查询方法
     *
     * @param formValues
     * @param session
     * @return
     */
    @RequestMapping("selectAllDataByTemplete")
    @ResponseBody
    public PagedObject<List<Map<String, Object>>> selectAllDataByTemplete(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return commonqueryService.selectAllDataByTemplete(formValues.getData(), formValues.getPage(), formValues.getLimit(), formValues.getAllcount(), session);
    }

    /**
     * 预览查询
     *
     * @param formValues
     * @param session
     * @return
     */
    @RequestMapping("selectAllDataByTempleteReview")
    @ResponseBody
    public PagedObject<List<Map<String, Object>>> selectAllDataByTempleteReview(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return commonqueryService.selectAllDataByTempleteReview(formValues.getData(), formValues.getPage(), formValues.getLimit(), formValues.getAllcount(), session);
    }

    /**
     * 根据父ID获取所有的子节点
     *
     * @param formValues
     * @param session
     * @return
     */
    @RequestMapping("selectChildBypId")
    @ResponseBody
    public PagedObject<List<BaseCommonquery>> selectChildBypId(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return commonqueryService.selectChildBypId(formValues.getData(), formValues.getPage(), formValues.getLimit(), formValues.getAllcount(), session);
    }

    /**
     * 根据表头的Id获取所有的结果集合
     *
     * @param formValues
     * @param session
     * @return
     */
    @RequestMapping("selectResultBypId")
    @ResponseBody
    public PagedObject<List<Map<String, Object>>> selectResultBypId(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return commonqueryService.selectResultBypId(formValues.getData(), formValues.getPage(), formValues.getLimit(), formValues.getAllcount(), session);
    }

    /**
     * 根据表头的Id获取所有的条件集合
     *
     * @param formValues
     * @param session
     * @return
     */
    @RequestMapping("selectConditionBypId")
    @ResponseBody
    public PagedObject<List<Map<String, Object>>> selectConditionBypId(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return commonqueryService.selectConditionBypId(formValues.getData(), formValues.getPage(), formValues.getLimit(), formValues.getAllcount(), session);
    }

    /**
     * 根据表头的Id获取所有的条件集合
     *
     * @return
     */
    @RequestMapping("caculateDate")
    @ResponseBody
    public ResultObject caculateDate(@RequestBody Map<String, Object> param) {
        return commonqueryService.caculateDate(param);
    }

    @RequestMapping("getNameByPageId")
    @ResponseBody
    public ResultObject getNameByPageId(@RequestBody String pageId, HttpSession session) {
        return commonqueryService.getNameByPageId(pageId);
    }

    /**
     * 下载生成的临时文件
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/downloadTmp")
    @ResponseBody
    public void downloadTmp(HttpServletRequest request, HttpServletResponse response) {
        new FileHandlerImpl().downloadFile(request, response, FilePathType.RELATIVE, "Resources\\static\\entem\\currencyTemplete\\currencyTempleteFile\\", FileExecuteType.DELETE_AFTER, FileContentType.XLS);
    }
}
